{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ccache"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import shutil\n",
    "from pathlib import Path\n",
    "import tempfile\n",
    "import tvm\n",
    "from tvm.contrib.cc import create_shared, create_executable, _is_linux_like, _is_windows_like"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def _src_gen(text):\n",
    "    return \"\"\"\n",
    "#include <iostream>\n",
    "\n",
    "int main() {\n",
    "    std::cout << \"text\";\n",
    "    return 0;\n",
    "}\"\"\".replace(\n",
    "        \"text\", text\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def _compile(f_create, text, output):\n",
    "    with tempfile.TemporaryDirectory() as temp_dir:\n",
    "        temp_dir = Path(temp_dir)\n",
    "        src_path = temp_dir/\"src.cpp\"\n",
    "        with open(src_path, \"w\", encoding=\"utf-8\") as file:\n",
    "            file.write(_src_gen(text))\n",
    "        log_path = temp_dir/\"log.txt\"\n",
    "        ccache_env = {\n",
    "            \"CCACHE_COMPILERCHECK\": \"content\",\n",
    "            \"CCACHE_LOGFILE\": log_path,\n",
    "        }\n",
    "        f_create(output, [\"src.cpp\"], [\"-c\"], cwd=temp_dir, ccache_env=ccache_env)\n",
    "        with open(log_path, \"r\", encoding=\"utf-8\") as file:\n",
    "            log = file.read()\n",
    "        return log"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/usr/bin/ccache'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shutil.which(\"ccache\") # 查看安装位置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 测试共享"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "if _is_linux_like():\n",
    "    _ = _compile(create_shared, \"shared\", \"main.o\")\n",
    "    log = _compile(create_shared, \"shared\", \"main.o\")\n",
    "    assert \"Succeeded getting cached result\" in log\n",
    "elif _is_windows_like():\n",
    "    _ = _compile(create_shared, \"shared\", \"main.obj\")\n",
    "    log = _compile(create_shared, \"shared\", \"main.obj\")\n",
    "    assert \"Succeeded getting cached result\" in log"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2025-05-16T15:27:35.389101 995845] === CCACHE 3.7.7 STARTED =========================================\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) base_dir = \n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) cache_dir = /home/ai/.ccache\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) cache_dir_levels = 2\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) compiler = \n",
      "[2025-05-16T15:27:35.389101 995845] Config: (environment) compiler_check = content\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) compression = false\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) compression_level = 6\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) cpp_extension = \n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) debug = false\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) depend_mode = false\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) direct_mode = true\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) disable = false\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) extra_files_to_hash = \n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) hard_link = false\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) hash_dir = true\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) ignore_headers_in_manifest = \n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) keep_comments_cpp = false\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) limit_multiple = 0.8\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (environment) log_file = /tmp/tmpf6wi1lka/log.txt\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) max_files = 0\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (/home/ai/.ccache/ccache.conf) max_size = 5.0G\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) path = \n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) pch_external_checksum = false\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) prefix_command = \n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) prefix_command_cpp = \n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) read_only = false\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) read_only_direct = false\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) recache = false\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) run_second_cpp = true\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) sloppiness = \n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) stats = true\n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) temporary_dir = \n",
      "[2025-05-16T15:27:35.389101 995845] Config: (default) umask = \n",
      "[2025-05-16T15:27:35.389162 995845] Command line: ccache /media/pc/data/lxw/envs/anaconda3a/envs/ai/bin/x86_64-conda-linux-gnu-c++ -shared -fPIC -o main.o src.cpp -c\n",
      "[2025-05-16T15:27:35.389172 995845] Hostname: Alg\n",
      "[2025-05-16T15:27:35.389189 995845] Working directory: /tmp/tmpf6wi1lka\n",
      "[2025-05-16T15:27:35.389216 995845] Source file: src.cpp\n",
      "[2025-05-16T15:27:35.389221 995845] Object file: main.o\n",
      "[2025-05-16T15:27:35.391639 995845] Trying direct lookup\n",
      "[2025-05-16T15:27:35.391668 995845] Looking for object file hash in /home/ai/.ccache/9/9/baa259ab40c58c274e7c1946edd857-1723286.manifest\n",
      "[2025-05-16T15:27:35.398058 995845] Got object file hash from manifest\n",
      "[2025-05-16T15:27:35.398124 995845] Copying /home/ai/.ccache/e/2/126b3df1985c57a73bd0ef5d5d9af7-2712409.o to main.o via main.o.Alg.995845.YVFGE4 (uncompressed)\n",
      "[2025-05-16T15:27:35.398170 995845] Created from cache: /home/ai/.ccache/e/2/126b3df1985c57a73bd0ef5d5d9af7-2712409.o -> main.o\n",
      "[2025-05-16T15:27:35.398186 995845] Succeeded getting cached result\n",
      "[2025-05-16T15:27:35.398193 995845] Result: cache hit (direct)\n",
      "[2025-05-16T15:27:35.398212 995845] Acquired lock /home/ai/.ccache/e/stats.lock\n",
      "[2025-05-16T15:27:35.398330 995845] Releasing lock /home/ai/.ccache/e/stats.lock\n",
      "[2025-05-16T15:27:35.398338 995845] Unlink /home/ai/.ccache/e/stats.lock\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(log)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 测试 executable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "if _is_linux_like():\n",
    "    _ = _compile(create_executable, \"executable\", \"main\")\n",
    "    log = _compile(create_executable, \"executable\", \"main\")\n",
    "    assert \"Succeeded getting cached result\" in log\n",
    "elif _is_windows_like():\n",
    "    _ = _compile(create_executable, \"executable\", \"main.exe\")\n",
    "    log = _compile(create_executable, \"executable\", \"main.exe\")\n",
    "    assert \"Succeeded getting cached result\" in log"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ai",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
